package config;

public class FullMiddleManConfiguration {
	private MiddleManConfiguration conf;
	private String middleManHost;
	
	public FullMiddleManConfiguration(String _middlemanHost, int _P, int _L, int _M, String[] _databases) {
		middleManHost = _middlemanHost;
		conf = new MiddleManConfiguration(_P, _L, _M, _databases);
	}
	
	public FullMiddleManConfiguration(String a_str) {
		if(a_str == null || a_str.startsWith("{")==false || a_str.endsWith("}")==false) {
			throw new IllegalArgumentException("[Doesn't start with { or end with } or is null]:\nString '"+a_str+"' is not a valid FullMiddleManConfiguration string");
		}
		// Eliminate { and }
		String str = a_str.substring(1, a_str.length()-1);
		
		String []pieces = str.split(";");
		if(pieces.length != 2) {
			throw new IllegalArgumentException("[Doesn't have two main pieces]:\nString '"+str+"' is not a valid FullMiddleManConfiguration string");
		}
		String host_piece = pieces[0];
		String other_piece = pieces[1];
		
		if(host_piece.startsWith("{") == false || host_piece.endsWith("}") == false) {
			throw new IllegalArgumentException("[Piece '"+host_piece+"' doesn't begin with { or end with }]:\nString '"+str+"' is not a valid FullMiddleManConfiguration string");
		}
		host_piece = host_piece.substring(1, host_piece.length()-1);
		String []host_pieces = host_piece.split(":");
		if(host_pieces.length != 2) {
			throw new IllegalArgumentException("[Piece '"+host_piece+"' doesn't have two pieces]:\nString '"+str+"' is not a valid FullMiddleManConfiguration string");
		}
		if(host_pieces[0].equals("middle_man") == false) {
			throw new IllegalArgumentException("[Piece '"+host_piece+"' doesn't have 'middle_man' as param name]:\nString '"+str+"' is not a valid FullMiddleManConfiguration string");
		}
		middleManHost = host_pieces[1];
		conf = new MiddleManConfiguration(other_piece);
	}
	
	public String middleManHost() {
		return middleManHost;
	}
	
	public MiddleManConfiguration getMiddleManConfiguration() {
		return conf;
	}
	
	public String toString() {
		String ret = "";
		ret += "{";
			ret += "{middle_man:"+middleManHost+"};";
			ret += conf.toString();
		ret += "}";
		return ret;
	}
}
